package cn.remex.task.model;

import cn.remex.db.rsql.model.ModelableImpl;
import cn.remex.task.TaskCenter;
import cn.remex.task.TaskConst;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import java.util.Date;
import java.util.List;


/**
 * @author liuhengyang
 *         date 2016-12-23 下午6:22:25
 * @version 版本号码
 */
@Table(
        uniqueConstraints = {
                //一个类型的分析器只能对应一个外部来源
                @UniqueConstraint(columnNames = {"taskType", "relationId"})
        }
)
public class SysTask extends ModelableImpl implements TaskConst{
    //用于唯一标示配置及分析器的key
    public String key(){
        return getTaskType() + "_" + getRelationId();
    }
    public boolean checkTimeout(Date heartbeatTime) {
        return null==this.heartbeatTime || heartbeatTime.getTime() - this.heartbeatTime.getTime() > TaskCenter.timeout;
    }

    //构造函数，方便点。
    public SysTask(String TaskType, String relationId, String analyzerJobParams) {
        this.taskType = TaskType;
        this.relationId = relationId;
        this.params = analyzerJobParams;
    }
    public SysTask() {
    }

    //关键数据库属性
    private String taskType;//分析器的类型
    private String relationId;//外部信息关键主键
    private String relationType;//外部信息类型
    @Column(columnDefinition = "CLOB")
    private String params;//job参数
    @OneToMany(mappedBy = "sysTask", cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
    private List<SysMatter> sysMatters;
    private ProcessStatus processStatus;//外部信息类型
    //运行状态属性，用于分布式控制的
    private String machineNode;
    private Date heartbeatTime;
    private Date expectStartTime = new Date();

    @Column(columnDefinition = "CLOB")
    private String processResult;

    ///////getter and setter///////

    public List<SysMatter> getSysMatters() {
        return sysMatters;
    }

    public void setSysMatters(List<SysMatter> sysMatters) {
        this.sysMatters = sysMatters;
    }

    public String getParams() {
        return params;
    }
    public void setParams(String params) {
        this.params = params;
    }
    public String getMachineNode() {
        return machineNode;
    }
    public void setMachineNode(String machineNode) {
        this.machineNode = machineNode;
    }
    public String getRelationType() {
        return relationType;
    }
    public void setRelationType(String relationType) {
        this.relationType = relationType;
    }
    public ProcessStatus getProcessStatus() {
        return processStatus;
    }
    public void setProcessStatus(
            ProcessStatus processStatus) {
        this.processStatus = processStatus;
    }
    public String getTaskType() {
        return taskType;
    }
    public void setTaskType(String TaskType) {
        this.taskType = TaskType;
    }
    public String getRelationId() {
        return relationId;
    }
    public void setRelationId(String relationId) {
        this.relationId = relationId;
    }
    public Date getHeartbeatTime() {
        return heartbeatTime;
    }
    public void setHeartbeatTime(Date heartbeatTime) {
        this.heartbeatTime = heartbeatTime;
    }

    public Date getExpectStartTime() {
        return expectStartTime;
    }

    public void setExpectStartTime(Date expectStartTime) {
        this.expectStartTime = expectStartTime;
    }

    public String getProcessResult() {
        return processResult;
    }

    public void setProcessResult(String processResult) {
        this.processResult = processResult;
    }
}
